<?php
/**
 * Descript: 用户列表
 * User: lufeng501206@gmail.com
 * Date: 2017/3/26 18:17
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Home\Orders;
use App\Models\Home\User;
use App\Models\MonthBalance;
use App\Repositories\TasksRepos;
use App\Repositories\UsersRepos;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    private $usersRepos = null;
    private $tasksRepos = null;
    private $pageLimit = 10;

    public function __construct(UsersRepos $usersRepos, TasksRepos $tasksRepos)
    {
        $this->middleware('verify.admin');
        $this->usersRepos = $usersRepos;
        $this->tasksRepos = $tasksRepos;
    }

    /**
     * 用户列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $data['auth'] = $this->getUserAuth();
        return view('system.user-lists',$data);
    }

    /**
     * 获取用户列表数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse\
     */
    public function getLists(Request $request)
    {
        $inputs = $request->input();
        $conditions['start'] = !empty($inputs['current']) ? ($inputs['current'] - 1) * $this->pageLimit : 0;
        $conditions['limit'] = $this->pageLimit;
        if (!empty($inputs['type']) && !empty($inputs['keyword'])) {
            $conditions[$inputs['type']] = $inputs['keyword'];
        }
        $data = $this->usersRepos->getUsersLists($conditions, false);
        $data['identity'] = config('jinbell.identity');
        return successJson($data);
    }

    /**
     * 禁用用户
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse\
     */
    public function forbid(Request $request)
    {
        $input = $request->input();
        $state = $input['state'];
        $userLists = $input['user_lists'];
        if (!empty($userLists) && is_array($userLists)) {
            foreach ($userLists as $id => $v) {
                if ($v) {
                    $userModel = User::findOrFail($id);
                    $userModel->state = $state;
                    $userModel->save();
                    // 消息触发
                    if ($state == 1) {
                        $msgTitle = '解封通知';
                    }elseif ($state == 2) {
                        $msgTitle = '封号通知';
                    }
                    $msgData['${user_name}'] = $userModel->account;
                    $this->usersRepos->triggerMessage($id,$msgTitle,$msgData);

                }
            }
        }
        return successJson();
    }

    public function show($id)
    {
        $data['id'] = $id;
        $data['auth'] = $this->getUserAuth();
        return view('system.user-info', $data);
    }

    /**
     * 获取用户概况
     * @param $id
     * @return \Illuminate\Http\JsonResponse\
     */
    public function getUserInfo($id)
    {
        $data['detail'] = $this->usersRepos->getUserDetailInfo($id);
        $data['identity'] = config('jinbell.identity');
        return successJson($data);
    }

    /**
     * 用户的任务列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function taskLists($id)
    {
        $data['id'] = $id;
        $data['auth'] = $this->getUserAuth();
        return view('admin.users.task-lists', $data);
    }

    /**
     * 获取用户的任务列表
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse\
     */
    public function getTaskLists(Request $request, $id)
    {
        $lists = $this->tasksRepos->getTasksListsByUserId($id);
        $data['lists'] = $lists;
        return successJson($data);
    }

    /**
     * 提现记录
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse\
     */
    public function getCashLists(Request $request, $id)
    {
        $conditions['user_id'] = $id;
        $lists = $this->tasksRepos->getCashLists($conditions);
        $data['lists'] = $lists;
        return successJson($data);
    }

    /**
     * 结算记录
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse\
     */
    public function getBalanceLists(Request $request, $id)
    {
        // 获取结算列表
        $userid = $id;
        $conditions['state'] = 4;
        // 获取完成任务
        $userTaskLists = $this->tasksRepos->getTasksListsByUserId($userid,$conditions);
        // 获取提成任务
        $recommendUserTaskLists = [];
        // 获取好友列表
        $userInfo = $this->usersRepos->getUserInfo($userid);
        $recommendUserLists = $this->usersRepos->getRecommendUserLists($userInfo['account']);
        if (!empty($recommendUserLists) && is_array($recommendUserLists)) {
            foreach ($recommendUserLists as $key => $value) {
                $lists = $this->tasksRepos->getTasksListsByUserId($value['id'],$conditions);
                $recommendUserTaskLists = array_merge($recommendUserTaskLists,$lists);
            }
        }
        // 汇总数据
        $allTaskLists = array_merge($userTaskLists,$recommendUserTaskLists);
        // 返回数据
        $returnData = [];
        if (!empty($allTaskLists) && is_array($allTaskLists)) {
            foreach ($allTaskLists as $key => $value) {
                $temp = [];
                $month = date("Y-m",strtotime($value['updated_at']));
                if ($value['user_id'] == $userid) {
                    $temp['money'] = $value['money'];
                } else {
                    $temp['money'] = $value['rebate'];
                }
                if (empty($returnData[$month])) {
                    $returnData[$month] = [];
                    $returnData[$month]['total_nums'] = 0;
                    $returnData[$month]['total_cash_money'] = 0;
                }
                $returnData[$month]['month'] = $month ;
                $returnData[$month]['total_nums'] ++ ;
                $returnData[$month]['total_cash_money'] += $temp['money'];
            }
        }
        $lists = $returnData;
        ksort($lists);
        return successJson($lists);
//        $lists = MonthBalance::where('user_id',$id)->get()->toArray();
//        $data['lists'] = $lists;
//        return successJson($data);
    }

    public function getMonthDetails(Request $request, $id)
    {
        $month = $request->input('month');
        $userid = $id;
        $conditions['month'] = $month;
        $conditions['state'] = 4;
        // 获取完成任务
        $userTaskLists = $this->tasksRepos->getTasksListsByUserId($userid,$conditions);
        // 获取提成任务
        $recommendUserTaskLists = [];
        // 获取好友列表
        $userInfo = $this->usersRepos->getUserInfo($userid);
        $recommendUserLists = $this->usersRepos->getRecommendUserLists($userInfo['account']);
        if (!empty($recommendUserLists) && is_array($recommendUserLists)) {
            foreach ($recommendUserLists as $key => $value) {
                $lists = $this->tasksRepos->getTasksListsByUserId($value['id'],$conditions);
                $recommendUserTaskLists = array_merge($recommendUserTaskLists,$lists);
            }
        }
        // 汇总数据
        $allTaskLists = array_merge($userTaskLists,$recommendUserTaskLists);
        // 返回数据
        $returnData = [];
        if (!empty($allTaskLists) && is_array($allTaskLists)) {
            foreach ($allTaskLists as $key => $value) {
                $temp = [];
                $temp['title'] = $value['title'];
                $temp['time'] = $value['updated_at'];
                $temp['timestamp'] = strtotime($value['updated_at']);
                $temp['username'] = $value['name'];
                if ($value['user_id'] == $userid) {
                    $temp['type_name'] = "任务获得";
                    $temp['money'] = $value['money'];
                } else {
                    $temp['type_name'] = "推荐奖励";
                    $temp['money'] = $value['rebate'];
                }
                $returnData[] = $temp;
            }
        }
        if (!empty($returnData)) {
            $sort = array_column($returnData, 'timestamp');
            array_multisort($sort, SORT_ASC, $returnData);
        }
        return successJson($returnData);
    }

    /**
     * 获取好友列表
     * @param $id
     * @return \Illuminate\Http\JsonResponse\
     */
    public function getFriendLists($id)
    {
        $userInfo = $this->usersRepos->getUserInfo($id);
        $data['lists'] = $this->usersRepos->getFriendLists($userInfo['account']);
        return successJson($data);
    }

    /**
     * 查询用户登录日志
     * @param $id
     * @return \Illuminate\Http\JsonResponse\
     */
    public function getUserLoginLogs($id)
    {
        $userInfo = $this->usersRepos->getUserInfo($id);
        $data['lists'] = $this->usersRepos->getUserLoginLogs($userInfo['account']);
        return successJson($data);
    }


}